<?php
class Admin_NguoiDungController extends Zend_Controller_Action
{
	public function init()
	{
		$this->_arrParam = $this->_request->getParams();
		$this->view->arrParam = $this->_arrParam;
	}
	public function indexAction()
	{
		$nguoidungMapper = Data_Model_NguoiDungMapper::getInstance();
		$nguoidungs = $nguoidungMapper->nguoidungList();
		if($this->getRequest()->isPost())
		{
			foreach($nguoidungs as $nguoidung)
			{
				$id = $nguoidung->getId();
				$chon = $this->_arrParam['case'.$id];
				if($chon == '1')
				{
					$nguoidungMapper->delete($id);
				}
			}
			
			$this->_helper->flashMessenger->addMessage(array('green'=>'Xóa người dùng thành công'));
			
			$this->_redirect('/admin/nguoidung/index');
		}
		$this->view->nguoidung = $nguoidungs;
	}
	public function themAction()
	{
		$this->view->title = 'Thêm người dùng';
		$formThem = new Admin_Form_ThemNDForm();
		
		$formThem->setMethod('post');
		$formThem->setAction('/QLHV/public/admin/nguoidung/them/');
		
		$formThem->addElement('submit','submit',array('label'=>'Thêm'));
		$this->view->form = $formThem;
		if($this->getRequest()->isPost())
		{
			if($formThem->isValid($_POST))
			{
				$data = $formThem->getValues();
				
				$tennd 		= $data['tennguoidung'];
				$email 		= $data['email'];
				$tendn		= $data['tendangnhap'];
				$matkhau 	= md5($data['matkhau']);
				$khoa 		= $data['khoa'];
				$quyenhan 	= $data['quyenhan'];
				$nguoidungMapper = Data_Model_NguoiDungMapper::getInstance();
				$nguoidungObj = new Data_Model_NguoiDung();

				$nguoidungObj->setTennd($tennd);
				$nguoidungObj->setEmail($email);
				$nguoidungObj->setTendangnhap($tendn);
				$nguoidungObj->setMatkhau($matkhau);
				$nguoidungObj->setKhoa($khoa);
				$nguoidungObj->setQuyenhan($quyenhan);
					 
				$nguoidung = $nguoidungMapper->save($nguoidungObj);
				
				$this->_helper->flashMessenger->addMessage(array('green'=>'Thêm người dùng thành công'));
				
				$this->_redirect('/admin/nguoidung/index');

			}
		}
	}
	public function capnhatAction()
	{
		$user_id = $this->getRequest()->getParam('id');
		$this->view->title = 'Cập nhật người dùng';
		$formCapnhat = new Admin_Form_ThemNDForm($user_id);
		
		$formCapnhat->setMethod('post');
		$formCapnhat->setAction('/QLHV/public/admin/nguoidung/capnhat/');
		
		$formCapnhat->addElement('submit','submit',array('label'=>'Lưu'));
		$formCapnhat->addElement('hidden','id');
		$nguoidungMapper = Data_Model_NguoiDungMapper::getInstance();
		
		$this->view->form = $formCapnhat;
		if($this->getRequest()->isPost())
		{
			if($formCapnhat->isValid($_POST))
			{
				$data = $formCapnhat->getValues();
				
				$id 		= $data['id'];
				$tennd 		= $data['tennguoidung'];
				$email 		= $data['email'];
				$tendn		= $data['tendangnhap'];
				$matkhau 	= md5($data['matkhau']);
				$khoa 		= $data['khoa'];
				$quyenhan 	= $data['quyenhan'];
		
				
				$nguoidungObj = new Data_Model_NguoiDung();
				
				$nguoidungObj->setId($id);
				$nguoidungObj->setTennd($tennd);
				$nguoidungObj->setEmail($email);
				$nguoidungObj->setTendangnhap($tendn);
				$nguoidungObj->setMatkhau($matkhau);
				$nguoidungObj->setKhoa($khoa);
				$nguoidungObj->setQuyenhan($quyenhan);
		
				$nguoidung = $nguoidungMapper->save($nguoidungObj);
		
				$this->_helper->flashMessenger->addMessage(array('green'=>'Cập nhật người dùng thành công'));
				
				$this->_redirect('/admin/nguoidung/index');
		
			}
		}
		else {
			$user = $nguoidungMapper->searchById($user_id);
				
			$formCapnhat->getElement('id')->setValue($user->getId());
			$formCapnhat->getElement('tennguoidung')->setValue($user->getTennd());
			$formCapnhat->getElement('email')->setValue($user->getEmail());
			$formCapnhat->getElement('tendangnhap')->setValue($user->getTendangnhap());
			$formCapnhat->getElement('matkhau')->setValue($user->getMatkhau());
			$formCapnhat->getElement('xacnhanmatkhau')->setValue($user->getMatkhau());
			$formCapnhat->getElement('khoa')->setValue($user->getKhoa());
			$formCapnhat->getElement('quyenhan')->setValue($user->getQuyenhan());

		}
	}
	public function xoaAction()
	{
		$this->_helper->viewRenderer->setNoRender(true);
		$mand = $this->getRequest()->getParam('id');
		$nguoidungMapper = Data_Model_NguoiDungMapper::getInstance();
		$nguoidungMapper->delete($mand);
		
		$this->_helper->flashMessenger->addMessage(array('green'=>'Xóa người dùng thành công'));
		
		$this->_redirect('/admin/nguoidung/index');
	}
	
	public function validateformAction()
	{
		$user_id = $this->getRequest()->getParam('id');
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		$form = new Admin_Form_ThemNDForm($user_id);
		$form->isValid($this->_getAllParams());
		$json = $form->getMessages();
		header('Conten-type: application/json');
		echo Zend_Json::encode($json);
	}
}